/******************************************************************
 * Created by: GOH CHEE HONG                                      
 * Date of Creation: 1/Jan/2009     
 * Date of last modification: 
 *                                                                
 * Description: Feeds form that retrieves all feeds from the peer
 * Acknowledgement: Most of the source codes are adopted from the feedexpress
 ******************************************************************/
/*
 * FeedExpress
 * (C) Copyright 2003 Jeppe Cramon (jeppe@cramon.dk)
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * Disclaimer:
 * -----------
 * This software is provided "as is" without warranty of any kind,
 * either expressed or implied. The entire risk as to the
 * quality and performance of the software is with you. Should the
 * software prove defective, you assume the cost of all necessary
 * servicing, repair, or correction. In no event shall the author,
 * copyright holder, or any other party who may redistribute the
 * software be liable to you for damages, including any general,
 * special, incidental, or consequential damages arising out of
 * the use or inability to use the software (including, but not
 * limited to, loss of data, data being rendered inaccurate, loss of
 * business profits, loss of business information, business
 * interruptions, loss sustained by you or third parties, or a
 * failure of the software to operate with any other software) even
 * if the author, copyright holder, or other party has been advised
 * of the possibility of such damages. 
 * 
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using Rss;
using System.Threading;
using Pbdms.Common.BaseClass;
namespace Pbdms.Forms.Views
{
    public partial class FrmViewFeeds : newForms
    {
        private static Mutex mut = new Mutex();
        public  BusinessLogic.RSS.SubscriptionManager subscManager = new Pbdms.BusinessLogic.RSS.SubscriptionManager(Program.networkCtrller);
        public delegate void DelegateWorker(object sender, System.EventArgs e);
        public delegate void DelegateStringWorker(object sender, string e);
        // Used to keep track of which Subscription were viewing
        private Pbdms.BusinessLogic.RSS.Subscription mActiveSubscription=null;
        private Pbdms.Common.ListViewColumnSorter listViewColumnSorter;
 
        // Used to keep track of which feed was last viewed (for marking a feed as not read)
        private Pbdms.BusinessLogic.RSS.FeedEntry mLastViewedFeed = null;
        private ListViewItem mLastViewedListViewItem = null;
        public FrmViewFeeds()
        {
            InitializeComponent();
            this.FormClosing += new FormClosingEventHandler(FrmViewFeed_FormClosing);
            
            // setup sorting
            listViewColumnSorter = new Pbdms.Common.ListViewColumnSorter();
            lstFeeds.ListViewItemSorter = listViewColumnSorter;
            listViewColumnSorter.Order = SortOrder.None;
            listViewColumnSorter.SortColumn = 0;
            listViewColumnSorter.DateColumn = 0;
            lstFeeds.Sort();
            subscManager.SubscriptionRemoved += new EventHandler(OnSubscriptionRemoved);
            subscManager.SubscriptionAdded += new EventHandler(OnSubscriptionAdded);
            subscManager.RefreshStart += new EventHandler(OnRefreshStart);
            subscManager.RefreshDone += new EventHandler(OnRefreshDone);
            subscManager.RefreshFailed += new EventHandler(OnRefreshFailed);
            subscManager.RefreshProcess += new Pbdms.BusinessLogic.RSS.SubscriptionManager.RefreshProcessHandler(OnRefreshProcess);
            subscManager.NewFeeds += new EventHandler(OnNewFeeds);
            treeFeeds.MouseUp += new MouseEventHandler(treeFeeds_MouseUp);
            treeFeeds.AfterSelect+=new TreeViewEventHandler(treeFeeds_AfterSelect);
            lstFeeds.Click += new EventHandler(listFeeds_Click);
            lstFeeds.ColumnClick += new ColumnClickEventHandler(lstFeeds_ColumnClick); ;
            Load_Feeds();
            subscManager.loadSubscription();
            Pbdms.Common.Global.paintBackGround(this);

        }
  
        public override void changeName(string keyID, string oldName,string newName)
        {
            ArrayList listSubsc=BusinessLogic.DataController.SubscriptionInfoController.create().getListOfSubscriptionsInfo();
            if (listSubsc == null)
            {
                return;
            }

            lock (this)
            {
                foreach (Common.DataObject.SubscriptionInfo subscription in listSubsc)
                {
                    if (subscription.FriendID.Equals(keyID))
                    {
                        TreeNode[] nodes = treeFeeds.Nodes.Find(subscription.FriendID + " " + subscription.Channel, true);
                        if (nodes == null)
                        {
                            return;
                        }
                        if (nodes.Length == 0)
                        {
                            return;
                        }
                        BusinessLogic.RSS.Subscription subscr = (BusinessLogic.RSS.Subscription)subscManager.arrSubscriptions[nodes[0].Name];

                        updateSubscriptionText(subscr, nodes[0]);

                    }
                }
                
            }
        }
        //initialise the left panel
        private void Load_Feeds()
        {

            TreeNode node1;
            node1 = treeFeeds.Nodes.Add("Newest Library additions");
            node1.Name = "Newest Library additions";
         

            
            lstFeeds.Columns[1].Width = lstFeeds.Width-lstFeeds.Columns[0].Width;

            //listvew initialisation 
            //lstRssFeed = new ListView();
            /*
            lstRssFeed.View = View.Details;
            ColumnHeader colHead = new ColumnHeader();
            colHead.Text = "title";

            lstRssFeed.Columns.Add(colHead);
           
            ColumnHeader colHead1 = new ColumnHeader();
           
            colHead1.Text = "Publish date";
            lstRssFeed.Columns.Add(colHead1);
            lstRssFeed.Columns[1].Width = -2;
            lstRssFeed.FullRowSelect = true;
            lstRssFeed.GridLines = true;
             */

        }
        //add subscriber event
        private void OnSubscriptionAdded(object source, EventArgs args)
        {
            //add subscriptioninfo
            BusinessLogic.RSS.Subscription subscription = (BusinessLogic.RSS.Subscription)source;
            TreeNode[] node = treeFeeds.Nodes.Find(subscription.info.Channel, false);
            if (node[0] != null)
            {
                string userid = BusinessLogic.DataController.BuddyController.create().selectIDBasedOnKeyIDFromDB(subscription.info.FriendID);
                Common.DataObject.Buddy buddy = BusinessLogic.DataController.BuddyController.create().selectABuddyBasedOnIDFromDB(userid);
                TreeNode temp = node[0].Nodes.Add(buddy.Name);
                temp.Name = subscription.cacheKey;
                updateSubscriptionText(subscription, temp);
                temp.EnsureVisible();
                treeFeeds.SelectedNode = temp;
                lstFeeds.Items.Clear();
                node[0].Expand();
                //simulate click
                treeFeeds_AfterSelect(this, new TreeViewEventArgs(temp, TreeViewAction.Unknown));
                showFeed(null);
            }
        }
        //remove a subscription
        private void OnSubscriptionRemoved(object source, EventArgs args)
        {
            TreeNode[] node = null;
 
                BusinessLogic.RSS.Subscription subscription = (BusinessLogic.RSS.Subscription)source;

                    node = treeFeeds.Nodes.Find(subscription.cacheKey, true);

                    node[0].Remove();
           

   
        }

        //uncompleted
        private void treeFeeds_AfterSelect(object sender, TreeViewEventArgs e)
        {
            panelNewAddition.Visible = false;
            panelDailyActiv.Visible = false;
            //if the selected node is the top node
            if (treeFeeds.SelectedNode.Parent == null)
            {
                lstFeeds.Items.Clear();
                showFeed(null);
                return;
            }
            //if the node is a  subnode
            string tag = e.Node.Name;
            Pbdms.BusinessLogic.RSS.Subscription subscription = (Pbdms.BusinessLogic.RSS.Subscription)subscManager.arrSubscriptions[tag];
            showSubscription(subscription);
        }
        
        
        //refresh starts
        private void OnRefreshStart(object source, EventArgs args)
        {
            try
            {
                //invoke GUI thread
                if (InvokeRequired)
                {
                    DelegateWorker delegateWorker = new DelegateWorker(OnRefreshStart);
                    this.Invoke(delegateWorker, new object[] { source, args });

                }
                else
                {
                    BusinessLogic.RSS.Subscription subscription = (BusinessLogic.RSS.Subscription)source;
                    string userid = BusinessLogic.DataController.BuddyController.create().selectIDBasedOnKeyIDFromDB(subscription.info.FriendID);
                    Common.DataObject.Buddy buddy = BusinessLogic.DataController.BuddyController.create().selectABuddyBasedOnIDFromDB(userid);
                    MainStatusbar.Text = "Downloading " + buddy.Name + "'s " + subscription.info.Channel + " starts";

                }
            }
            catch
            {

            }
          
        }
        private void OnRefreshProcess(object source, string args)
        {
            //invoke GUI thread
            try
            {
                if (InvokeRequired)
                {
                    DelegateStringWorker delegateWorker = new DelegateStringWorker(OnRefreshProcess);
                    this.Invoke(delegateWorker, new object[] { source, args });

                }
                else
                {
                    BusinessLogic.RSS.Subscription subscription = (BusinessLogic.RSS.Subscription)source;
                    TreeNode[] node = treeFeeds.Nodes.Find(subscription.cacheKey, true);
                    if (node[0] != null)
                    {
                        string userid = BusinessLogic.DataController.BuddyController.create().selectIDBasedOnKeyIDFromDB(subscription.info.FriendID);
                        Common.DataObject.Buddy buddy = BusinessLogic.DataController.BuddyController.create().selectABuddyBasedOnIDFromDB(userid);
                        node[0].Text = buddy.Name + " (" + args + "%)";
                        //MainStatusbar.Text = "Downloading " + buddy.Name + "'s " + subscription.info.Channel + " (" + args + "%)"; ;

                    }
                    else
                    {
                        throw new Exception("cannot update the tree node");
                    }

                }
            }
            catch
            {

            }
        }
        private void OnRefreshDone(object source, EventArgs args)
        {
            //invoke GUI thread
            try
            {
                if (InvokeRequired)
                {
                    DelegateWorker delegateWorker = new DelegateWorker(OnRefreshDone);
                    this.Invoke(delegateWorker, new object[] { source, args });

                }
                else
                {
                    BusinessLogic.RSS.Subscription subscription = (BusinessLogic.RSS.Subscription)source;
                    TreeNode[] node = treeFeeds.Nodes.Find(subscription.cacheKey, true);
                    if (node[0] != null)
                    {
                        string userid = BusinessLogic.DataController.BuddyController.create().selectIDBasedOnKeyIDFromDB(subscription.info.FriendID);
                        Common.DataObject.Buddy buddy = BusinessLogic.DataController.BuddyController.create().selectABuddyBasedOnIDFromDB(userid);

                        updateSubscriptionText(subscription, node[0]);


                        MainStatusbar.Text = "Finished downloading " + buddy.Name + "'s " + subscription.info.Channel;
                    }


                }
            }
            catch
            {

            }
        }
        //event new feeds
        private void OnNewFeeds(object source, EventArgs args)
        {
            //invoke GUI thread
            try
            {
                if (InvokeRequired)
                {
                    DelegateWorker delegateWorker = new DelegateWorker(OnNewFeeds);
                    this.Invoke(delegateWorker, new object[] { source, args });

                }
                else
                {
                    BusinessLogic.RSS.Subscription subscription = (BusinessLogic.RSS.Subscription)source;
                    TreeNode[] node = treeFeeds.Nodes.Find(subscription.cacheKey, true);
                    if (node != null)
                    {
                        if (node[0] != null)
                        {

                            updateSubscriptionText(subscription, node[0]);


                            try
                            {
                                if (treeFeeds.SelectedNode == null && treeFeeds.SelectedNode.Parent == null)
                                {
                                    updateSubscriptionText(subscription, node[0]);

                                    return;
                                }
                            }
                            catch (Exception e)
                            {

                                return;
                            }

                            Pbdms.BusinessLogic.RSS.Subscription selectedSubscription = (Pbdms.BusinessLogic.RSS.Subscription)subscManager.arrSubscriptions[(string)treeFeeds.SelectedNode.Name];
                            if (selectedSubscription.cacheKey == subscription.cacheKey)
                            {
                                // Add the feeds to the list view
                                if (subscription.feedManager.feeds == null)
                                {

                                    return;
                                }
                                updateListView(subscription);
                            }
                            updateSubscriptionText(subscription, node[0]);



                        }
                    }

                }
            }
            catch
            {


            }
        }
        //refresh failed downloading
        private void OnRefreshFailed(object source, EventArgs args)
        {
            //invoke GUI thread

            try
            {
                if (InvokeRequired)
                {
                    DelegateWorker delegateWorker = new DelegateWorker(OnRefreshFailed);
                    this.Invoke(delegateWorker, new object[] { source, args });

                }
                else
                {
                    BusinessLogic.RSS.Subscription subscription = (BusinessLogic.RSS.Subscription)source;
                    string userid = BusinessLogic.DataController.BuddyController.create().selectIDBasedOnKeyIDFromDB(subscription.info.FriendID);
                    Common.DataObject.Buddy buddy = BusinessLogic.DataController.BuddyController.create().selectABuddyBasedOnIDFromDB(userid);
                    MainStatusbar.Text = "Error in downloading " + buddy.Name + "'s " + subscription.info.Channel;
                }
            }
            catch
            {

            }
        }
        //form closing event
        private void FrmViewFeed_FormClosing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            e.Cancel = true;

            this.Hide();
            expand();

        }
        //form activated event
        private void FrmViewFeeds_FormActivated(object sender, EventArgs e)
        {
            expand();
        }
        //expand all node
        private void expand()
        {

            TreeNode[] node = treeFeeds.Nodes.Find("Newest Library additions", true);
            node[0].ExpandAll();


        }

        private void updateSubscriptionText(Pbdms.BusinessLogic.RSS.Subscription subscription, TreeNode node)
        {
            string userid = BusinessLogic.DataController.BuddyController.create().selectIDBasedOnKeyIDFromDB(subscription.info.FriendID);
            Common.DataObject.Buddy buddy = BusinessLogic.DataController.BuddyController.create().selectABuddyBasedOnIDFromDB(userid);
            node.Text = buddy.Name + " (" + subscription.feedManager.getUnreadCount() + "/" + subscription.feedManager.feeds.Count + ")";
        }

        private void treeFeeds_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                // Select the clicked node
                treeFeeds.SelectedNode = treeFeeds.GetNodeAt(e.X, e.Y);
                if (treeFeeds.SelectedNode != null && treeFeeds.SelectedNode.Parent != null)
                {
                    contextMenu.Show(treeFeeds, e.Location);
                }
            }
        }
             
    
        //taken from http://www.codeproject.com/KB/list/listviewautosize.aspx to solve the problem of resize
        
        protected override void WndProc(ref Message message)
        {
            const int WM_PAINT = 0xf;

            // if the control is in details view mode and columns

            // have been added, then intercept the WM_PAINT message

            // and reset the last column width to fill the list view

            switch (message.Msg)
            {
                case WM_PAINT:
                    if (this.lstFeeds.View == View.Details && this.lstFeeds.Columns.Count > 0)
                        this.lstFeeds.Columns[this.lstFeeds.Columns.Count - 1].Width = -2;
                    break;
            }

            // pass messages on to the base control for processing

            base.WndProc(ref message);
        }
         
        protected override void OnPaint(PaintEventArgs e)
        {
            // If there is an image and it has a location, 
            // paint it when the Form is repainted.
            
            base.OnPaint(e);
            expand();
        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {

        }

        private void DeleteSubscriptionMenu_Click(object sender, EventArgs e)
        {
            TreeNode node = treeFeeds.SelectedNode;
            Pbdms.BusinessLogic.RSS.Subscription subscription = (Pbdms.BusinessLogic.RSS.Subscription)subscManager.arrSubscriptions[node.Name];
            subscManager.removeSubscription(subscription);
        }

        private void RefreshMenu_Click(object sender, EventArgs e)
        {
            TreeNode node = treeFeeds.SelectedNode;
            if (node != null)
            {
                BusinessLogic.RSS.Subscription subscr = (BusinessLogic.RSS.Subscription)subscManager.arrSubscriptions[node.Name];
                subscr.feedManager.refreshFeeds();
            }
        }

        private void RefreshAllMenuItem_Click(object sender, EventArgs e)
        {
            subscManager.RefreshAll();
        }

        private void MarkReadMenuItem_Click(object sender, EventArgs e)
        {
            TreeNode node = treeFeeds.SelectedNode;
            if (node != null)
            {
                BusinessLogic.RSS.Subscription subscr = (BusinessLogic.RSS.Subscription)subscManager.arrSubscriptions[node.Name];
                subscr.feedManager.markAllAsRead();
                updateSubscriptionText(subscr, node);
                updateListView(subscr);
            }
        }

        private void MarkUnreadMenuItem_Click(object sender, EventArgs e)
        {
            TreeNode node = treeFeeds.SelectedNode;
            if (node != null)
            {
                BusinessLogic.RSS.Subscription subscr = (BusinessLogic.RSS.Subscription)subscManager.arrSubscriptions[node.Name];
                subscr.feedManager.markAllAsUnRead();
                updateSubscriptionText(subscr, node);
                updateListView(subscr);
            }
        }

        private void updateListView(BusinessLogic.RSS.Subscription subscription)
        {
            if (subscription == null)
            {
                
                return;
            }
            Console.WriteLine("before entering update list view");
            lock (this)
            {
                Console.WriteLine("update list view");
                lstFeeds.Items.Clear();
                foreach (Pbdms.BusinessLogic.RSS.FeedEntry feed in subscription.feedManager.feeds.Values)
                {
                    ListViewItem viewItem = new ListViewItem(feed.publicationDate.ToString());
                    viewItem.Tag = feed.cacheKey;
                    if (feed.isRead)
                    {
                        
                        viewItem.Font = new Font("Tahoma", 8.25f);
                    }
                    else
                    {
                       
                        viewItem.Font = new Font("Tahoma", 8.25f, FontStyle.Bold);
                    }
                    viewItem.SubItems.Add(feed.title);
                    //viewItem.SubItems.Add(feed.subject);
                    lstFeeds.Items.Add(viewItem);
                    
                }
                listViewColumnSorter.SortColumn = 0;
                listViewColumnSorter.Order = SortOrder.Descending;
                lstFeeds.Sort();
                panelDailyActiv.Visible = false;
                mActiveSubscription = subscription;
            }
            TreeNode[] node = treeFeeds.Nodes.Find(mActiveSubscription.cacheKey, true);
            updateSubscriptionText(mActiveSubscription,node[0]);
        }

        private void showFeed(Pbdms.BusinessLogic.RSS.FeedEntry feed)
        {
            Console.WriteLine("before entering feed");
            lock (this)
            {
                Console.WriteLine("show feed");
                if (mLastViewedFeed != null)
                {
                   markFeedAsRead(mLastViewedFeed, mLastViewedListViewItem);
                }
                mLastViewedFeed = feed;
                if (feed == null)
                {
                    //panelNewAddition.Visible = false;
                    panelDailyActiv.Visible = false;
                    panelNewAddition.Visible = false;
                    /*
                    subscriptionLabel.Text = "";
                    subjectLabel.Text = "";
                    dateLabel.Text = "";
                    categoryLabel.Text = "";
                     */
                }
                else
                {
                    if (feed.subscription.info.Channel.Equals("Recent activities"))
                    {
                        panelNewAddition.Visible = false;
                        panelDailyActiv.Visible = true;
                        panelDailyLBLsubsc.Text = feed.subscription.info.Channel;
                        panelDailyLBLtitle.Text = feed.title;
                        panelDailyLBLdate.Text = feed.publicationDate.ToString("r");
                        rchTxtBox.Rtf = feed.description;
                    }
                    else
                    {
                        panelDailyActiv.Visible = false;
                        panelNewAddition.Visible = true;
                        panelDailyActiv.Visible = true;
                        panelNewLBLsubsc.Text = feed.subscription.info.Channel;
                        panelNewLBLtitle.Text = feed.title;
                        panelNewLBLdate.Text = feed.publicationDate.ToString("r");
                        txtNew.Text = feed.description; //+ " "+feed.rssGuid.guid;
                        
                    }
                    /*
                    subscriptionLabel.Text = feed.subscription.channelInformation.title;
                    subjectLabel.Text = feed.title;
                    dateLabel.Text = feed.publicationDate.ToString();
                    categoryLabel.Text = feed.subject;
                     */
                }
                //mHasDisplayedFeedInWebBrowser = false;
            }
            //redirectBrowser("about:blank");

        }

        private void markFeedAsRead(Pbdms.BusinessLogic.RSS.FeedEntry feed, ListViewItem viewItem)
        {
            try
            {
                feed.isRead = true;

                if (viewItem != null)
                {
                    viewItem.Font = new Font("Tahoma", 8.25f);

                }

                string userid = BusinessLogic.DataController.BuddyController.create().selectIDBasedOnKeyIDFromDB(feed.subscription.info.FriendID);
                Common.DataObject.Buddy buddy = BusinessLogic.DataController.BuddyController.create().selectABuddyBasedOnIDFromDB(userid);
                TreeNode[] node = treeFeeds.Nodes.Find(feed.subscription.cacheKey, true);
                if (node != null &&node.Length!=0)
                {
                    updateSubscriptionText(feed.subscription, node[0]);
                }
            }
            catch
            {

            }
        }


        //show each subscription in details
        private void showSubscription(Pbdms.BusinessLogic.RSS.Subscription subscription)
        {
            // Add the feeds to the list view in case none have been loaded
            if (subscription.feedManager.feeds.Count == 0)
            {
                subscription.feedManager.refreshFeeds();
            }

            string userid = BusinessLogic.DataController.BuddyController.create().selectIDBasedOnKeyIDFromDB(subscription.info.FriendID);
            Common.DataObject.Buddy buddy = BusinessLogic.DataController.BuddyController.create().selectABuddyBasedOnIDFromDB(userid);
            lblMiniDetails.Text = "FeedExpress - " + buddy.Name+"'s"+subscription.info.Channel;
           
            updateListView(subscription);

         
        }

 

        private void lstFeeds_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
        {
            // Determine if clicked column is already the column that is being sorted.
            if (e.Column == listViewColumnSorter.SortColumn)
            {
                // Reverse the current sort direction for this column.
                if (listViewColumnSorter.Order == SortOrder.Ascending)
                {
                    listViewColumnSorter.Order = SortOrder.Descending;
                }
                else
                {
                    listViewColumnSorter.Order = SortOrder.Ascending;
                }
            }
            else
            {
                // Set the column number that is to be sorted; default to ascending.
                listViewColumnSorter.SortColumn = e.Column;
                listViewColumnSorter.Order = SortOrder.Ascending;
            }

            // Perform the sort with these new sort options.
            this.lstFeeds.Sort();
        }

        //clicking on it
        private void listFeeds_Click(object sender, EventArgs e)
        {
            if (lstFeeds.SelectedItems.Count == 0)
            {
                return;
            }
            foreach (ListViewItem viewItem in lstFeeds.SelectedItems)
            {
                string feedEntryTag = (string)viewItem.Tag;
                Pbdms.BusinessLogic.RSS.FeedEntry feed = (Pbdms.BusinessLogic.RSS.FeedEntry)mActiveSubscription.feedManager.feeds[feedEntryTag];
                feed.isRead = true;
                viewItem.Font = new Font("Tahoma", 8.25f);
                showFeed(feed);
            }
            TreeNode [] node= treeFeeds.Nodes.Find(mActiveSubscription.cacheKey,true);
            if (node!=null){
                updateSubscriptionText(mActiveSubscription,node[0]);
               
            }

        }

        private void btnMoreInfo_Click(object sender, EventArgs e)
        {
            string itemID = null;
            Pbdms.BusinessLogic.RSS.FeedEntry feed = null;
            try
            {
                
                
                foreach (ListViewItem viewItem in lstFeeds.SelectedItems)
                {

                    string feedEntryTag = (string)viewItem.Tag;
                    feed = (Pbdms.BusinessLogic.RSS.FeedEntry)mActiveSubscription.feedManager.feeds[feedEntryTag];
                    itemID = feed.rssGuid.guid;
                    Common.DataObject.Buddy buddy = null;
                    buddy = Program.friendControl.getAFriendByKeyID(feed.subscription.info.FriendID);
                    if (buddy != null)
                    {
                        string ipAdd = buddy.IpAdd;
                        if (ipAdd != null)
                        {

                            Program.mainForm.frmViewAnItem.MdiParent = this.MdiParent;
                            Program.mainForm.frmViewAnItem.setParameters(buddy,itemID);
                            Program.mainForm.frmViewAnItem.Start();
                            Program.mainForm.frmViewAnItem.Show();
                            Program.mainForm.frmViewAnItem.Focus();
                        }
                    }
                }
            }
            catch
            {

            }
            
            
            
           

        }
    }
}